import os
import time
import sys

SERVICE_LOG_DIRS = {
    'accounting': ('/opt/app/log',),
    'charging': ('/opt/app/log',),
    'charging-rt': ('/opt/app/billinglog', '/opt/app/log'),
    'check': ('/opt/app/log',),
    'collect': ('/opt/app/log',),
    'contract': ('/opt/app/log',),
    'manage': ('/opt/app/log',),
    'settlement': ('/opt/app/log',),
    'provider': ('/opt/app/billinglog', '/opt/app/log')
}

DAYS_TO_KEEP = 60

SECONDS_TO_KEEP = DAYS_TO_KEEP * 24 * 60 * 60


def delete_old_logs():
    log_dirs = SERVICE_LOG_DIRS.get(sys.argv[1])
    print('Get delete log_dirs:', log_dirs)
    if log_dirs:
        current_time = time.time()
        for dir_path in log_dirs:
            print("Start clearn: " + dir_path)
            for root, dirs, files in os.walk(dir_path):
                for fileName in files:
                    if fileName.endswith('.log.gz') or fileName.endswith('.log') or fileName.endswith('.hprof') or fileName.startswith('gc.log.'):
                        file_path = os.path.join(root, fileName)
                        if os.path.isfile(file_path) and os.path.getmtime(file_path) < current_time - SECONDS_TO_KEEP:
                            print("Deleting old log file: " + file_path)
                            os.remove(file_path)


delete_old_logs()
